<?php
require '../../common/initPage.php';
require '../../common/dbcon.php';
require '../../common/func.php';
require '../../common/showTips.php';
require '../../common/checkForm.php';

session_start();
//拦截未登录
filterLogout();

unset($_SESSION['addUserShowTips']);
if($_GET['action']=='adduser'){
    if(
        isNull(array($_POST['adduid'],$_POST['adduname'],$_POST['addupwd'])) &&
        checkUserID($_POST['adduid']) &&
        checkUsername($_POST['adduname']) &&
        checkPassword($_POST['addupwd']) &&
        $_POST['addupwd']==$_POST['addcomupwd']
    ){
        $adduid=handledata($_POST['adduid']);
        $adduname=handledata($_POST['adduname']);
        $addusalt=substr(uniqid(microtime()), -6);
        $addupwd=enpwd(handledata($_POST['addupwd']), $addusalt);
        $addusex=handledata($_POST['addusex']);
        $adduiden=handledata($_POST['adduiden']);
        if(inNewUser($mysqlCon,inNewUser,$adduid,$adduname,$addupwd,$addusex,$adduiden,$addusalt)){
            $_SESSION['addUserShowTips'][]='添加新用户成功';
            $_SESSION['addUsertipsType']='success';
        }else{
            $_SESSION['addUserShowTips'][]='学号/工号已存在';
            $_SESSION['addUsertipsType']='danger';
        }
    }else{
        if (!$_POST['adduid']) $_SESSION['addUserShowTips'][]='用户学号/工号不能为空';
        if (!$_POST['adduname']) $_SESSION['addUserShowTips'][]='用户名不能为空';
        if (!$_POST['addupwd']) $_SESSION['addUserShowTips'][]='初始密码不能为空';
        if (!checkUserID($_POST['adduid'])) $_SESSION['addUserShowTips'][]='学号/工号应由8位纯数字组成';
        if (!checkUsername($_POST['adduname'])) $_SESSION['addUserShowTips'][]='用户名应由2~16位汉字、英文字母、数字、下划线组成';
        if (!checkPassword($_POST['addupwd'])) $_SESSION['addUserShowTips'][]='初始密码应由6~16位英文字母，数字，下划线组成';
        if ($_POST['addupwd']!=$_POST['addcomupwd']) $_SESSION['addUserShowTips'][]='两次初始密码输入不一致';
        $_SESSION['addUsertipsType']='warning';
    }
}

$routerUrl='../../views/admin/adduser.html';
require '../../views/admin/template.html';